#include "uiutils.h"

#include <QtXml>
#include <QFile>

UiUtils::PathCondition UiUtils::CR_R_PT()
{
    return {"colorrole", "role", "PlaceholderText"};
}

int UiUtils::countOccurrences(const QString &filePath, const PathCondition &condition)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qCritical() << "Error: Cannot open file";
        return 0;
    }

    QXmlStreamReader reader(&file);

    int occurrences = 0;
    while (!reader.atEnd()) {
        auto token = reader.readNext();

        switch (token) {
        case QXmlStreamReader::StartElement: {
//            if (reader.name().toString() == "colorrole" && reader.attributes().value("role") == "PlaceholderText") {
            if (reader.name().toString() == condition.elementName && reader.attributes().value(condition.attrName).toString() == condition.attrValue) {
                reader.skipCurrentElement();
                occurrences++;
                continue;
            }
            break;
        }
        default:
            ;
        }
    }
    return occurrences;
}

QString UiUtils::removeByCondition(const QString &filePath, const PathCondition &condition)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qCritical() << "Error: Cannot open file";
        return QString();
    }

    QBuffer output;
    output.open(QIODevice::WriteOnly | QIODevice::Text);

    QXmlStreamReader reader(&file);
    QXmlStreamWriter writer(&output);

    writer.setAutoFormatting(true);

    bool skiped = false;
    while (!reader.atEnd()) {
        auto token = reader.readNext();

        switch (token) {
        case QXmlStreamReader::StartDocument: {
            writer.writeStartDocument();
            break;
        }
        case QXmlStreamReader::StartElement: {
//            if (reader.name().toString() == "colorrole" && reader.attributes().value("role") == "PlaceholderText") {
            if (reader.name().toString() == condition.elementName && reader.attributes().value(condition.attrName).toString() == condition.attrValue) {
                skiped = true;
                reader.skipCurrentElement();
                /// Fix: 该节点跳过时，前面已经追加过 '\n    ' 内容, 所以这里向前移除一个空白部分
                output.seek(output.pos()-1);
                qDebug() << "发现:" + reader.text().toString();
                continue;
            }

            writer.writeStartElement(reader.name().toString());
            foreach (const auto &attr, reader.attributes()) {
                writer.writeAttribute(attr.name().toString(), attr.value().toString());
            }
            break;
        }
        case QXmlStreamReader::EndElement: {
            writer.writeEndElement();
            break;
        }
        case QXmlStreamReader::Characters: {
            if (!skiped) {
                writer.writeCharacters(reader.text().toString());
            }
            skiped = false;
            break;
        }
        default:
            writer.writeCurrentToken(reader);
        }
    }

    file.close();
    return output.data();
}
